using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace ConfigFileTest
{
    public partial class Form1 : Form
    {
        private Config config;

        public Form1()
        {
            InitializeComponent();
            SetupButtons();
        }

        private void SetupButtons()
        {
            btnLoad.Enabled = config == null;
            btnSave.Enabled = config != null;
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            config = Config.Load();
            if (config != null)
            {                
                Type objectType = config.GetType();

                foreach (PropertyInfo prop in objectType.GetProperties())
                    tbLog.Text = String.Format("{0}:\t\t{1}\n", prop.Name, prop.GetValue(config, null)) + tbLog.Text;

                tbLog.Text = "Config file loaded!\nValues:\n" + tbLog.Text;
            }
            else
                tbLog.Text = "Could not load config file.\n" + tbLog.Text;

            SetupButtons();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (config.Save())
                tbLog.Text = "Config file saved!\n" + tbLog.Text;
            else
                tbLog.Text = "Could not save config file.\n" + tbLog.Text;
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            Random rand = new Random();
            config = new Config();

            config.ApplicationName = Guid.NewGuid().ToString().Substring(0, 8);
            config.MaxNumberDebugLines = rand.Next(20);
            config.StartApplicationMaximized = rand.Next(1) == 1;

            config.SerializedObject = new TestObject();
            config.SerializedObject.TestBool = rand.Next(1) == 1;
            config.SerializedObject.TestInt = rand.Next(10);
            config.SerializedObject.TestString = Guid.NewGuid().ToString().Substring(0, 8);

            SetupButtons();
        }
    }
}